let express=require('express')
let serve=express()
serve.use(express.static('./static'))

serve.use(express.json(),express.urlencoded())
const jwt = require('jsonwebtoken');
// token的秘钥
let secret = 'wssb996'
// 设置白名单
let white = ['/api/goods/dataType','/api/goods/getData','/api/goods/getShop','/api/user/login','/api/city/citylist','/api/user/register']
// 统一验证token
serve.use((req, res, next) => {
    // console.log(req._parsedUrl.pathname);
    let url = req._parsedUrl.pathname;
    if (white.includes(url)||url.includes('/api/user/check')|| url.includes('/api/goods/details')) {
        next();
        return
    }
    let token = req.headers.authorization;
    // 解析token
    jwt.verify(token, secret, (err, decode) => {
        if (err) {
            res.send({
                code: 400,
                msg:'未登录'
            })
            return
        } 
        // 把token解析出来之后，想要在其他的中间件使用，可以把解密出来的用户信息存储到req请求对象中
        let info = {
            ...decode
        }
        req.info = info;
        // 继续下一步，真正请求的接口
        next();
    })
})
// 引入测试接口
// let test=require('./api/test.js')
// serve.use('/test',test)
// 导入goods相关的文件
let goods = require('./api/goods')
serve.use('/api/goods',goods);
let user = require('./api/user')
serve.use('/api/user',user);
let city = require('./api/city')
serve.use('/api/city',city)
let car = require('./api/car')
serve.use('/api/car',car)
serve.listen(9091,()=>{
    console.log('后端9091端口已经开启');
})

